# -*- coding: utf-8 -*-
 
import hashlib
from base import UserCase, Runner, TestSuite

class Case(UserCase):
    def test_my_device_list(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        for i in range(3):
            pp = {
                'cpu_id': self.randchar(12),
                'name': self.randhan(8),
                'corp': id,
            }
            res = self.c.open('/api/device/create', pp)
            self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-device/list')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)


    def test_my_device_option(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        for i in range(5):
            pp = {
                'cpu_id': self.randchar(12),
                'name': self.randhan(8),
                'corp': id,
            }
            res = self.c.open('/api/device/create', pp)
            self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-device/option')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 5)


    def test_my_device_field_alias(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        for i in range(3):
            pp = {
                'cpu_id': self.randchar(12),
                'name': self.randhan(8),
                'corp': id,
            }
            res = self.c.open('/api/device/create', pp)
            self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-device/field-alias', {'cpu_id': pp['cpu_id']})
        self.assertEqual(res['code'], 0)


    def test_my_device_list_name(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        for i in range(3):
            pp = {
                'cpu_id': self.randchar(12),
                'name': self.randhan(8),
                'corp': id,
            }
            res = self.c.open('/api/device/create', pp)
            self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-device/list', {'name': pp['name']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)


    def test_my_device_list_id(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        device_id = None
        for i in range(3):
            pp = {
                'cpu_id': self.randchar(12),
                'name': self.randhan(8),
                'corp': id,
            }
            res = self.c.open('/api/device/create', pp)
            self.assertEqual(res['code'], 0)
            device_id = res['data']['id']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-device/list', {'id': device_id})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)


    def test_my_device_list_cpu_id(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        cpu_id = None
        for i in range(3):
            pp = {
                'cpu_id': self.randchar(12),
                'name': self.randhan(8),
                'corp': id,
            }
            res = self.c.open('/api/device/create', pp)
            self.assertEqual(res['code'], 0)
            cpu_id = res['data']['cpu_id']

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-device/list', {'cpu_id': cpu_id})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)


    def test_my_device_as_corp_normal(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']
        username = p['username']
        password = p['password']

        p = {
            'name': self.randhan(8),
            'corp': corp['id'],
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        device = res['data']

        res = self.c.open('/login', {'username': username, 'password': password})
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(3),
            'role': 'normal',
            'password': hashlib.md5(self.randchar(6)).hexdigest(),
            'username': self.randchar(8),
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user = res['data']['user_obj']
        user_username = p['username']
        user_password = p['password']

        p = {
            'user': user['id'],
            'device': device['id'],
        }
        res = self.c.open('/api/my-corp-user-device/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': user_username, 'password': user_password})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-device/list')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)


    def test_my_device_get_rule(self):

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        corp_id = res['data']['id']
        corp_username = p['username']
        corp_password = p['password']

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp_id,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        device_id = res['data']['id']

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)

        rule_id = res['data']['id']

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)

        rule_id2 = res['data']['id']

        p = {
            'id': device_id,
            'rule': ','.join([str(rule_id), str(rule_id2)])
        }
        res = self.c.open('/api/device/apply-rule', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/get-rule', {'id': device_id})
        self.assertEqual(res['code'], 0)

        a = set(x['id'] for x in res['data'])
        b = set([rule_id, rule_id2])
        self.assertEqual(a, b)

        res = self.c.open('/login', {'username': corp_username, 'password': corp_password})
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'role': 'normal',
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)
        user_id = res['data']['user_obj']['id']

        res = self.c.open('/api/my-corp-user-device/create', {'user': user_id, 'device': device_id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-device/get-rule', {'id': device_id})
        a = set(x['id'] for x in res['data'])
        b = set([rule_id, rule_id2])
        self.assertEqual(a, b)


test_list = [
    'test_my_device_list',
    'test_my_device_option',
    'test_my_device_field_alias',
    'test_my_device_list_name',
    'test_my_device_list_id',
    'test_my_device_list_cpu_id',
    'test_my_device_as_corp_normal',
    'test_my_device_get_rule',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



